﻿@using LeadMaster.Helpers
@model LeadMaster.ViewModels.ViewModelPurchaseOrders

<style type="text/css">
.RunningTotal
{
    font-family: 'Courier New', Courier, monospace; 
    color: #FF0000; 
    font-weight: bold;
    font-size: 16px;
}
</style>
<div id="purchaseorders">
<label style="font-family: Arial, Helvetica, sans-serif; font-size: larger; font-variant: small-caps; color: #666666">@Model.Lead.Company.Company</label>
<hr />
<div id="ShowPurchaseOrders">
<table class="listtable contactstable" cellspacing="0" cellpadding="0" style="width: 950px;">
<thead>
<tr>
<th align="left" style="width:300px;"><label>Ref</label></th>
<th align="left"><label>Volume Ordered</label></th>
<th align="left"><label>New Leads Supplied</label></th>
<th align="left"><label>New Leads Deficit</label></th>
<th align="left"><label>Returns</label></th>
<th align="left"><label>Invalid Returns</label></th>
<th align="left"><label>Replacements Supplied</label></th>
<th align="left"><label>Replacements Deficit</label></th>
<th align="left"><label>Total Deficit</label></th>
<th align="left"><label>Client Paid ?</label></th>
<th align="left"><label>Visible ?</label></th>
<th></th>
</tr>
</thead>

@foreach (var item in Model.POReportViewList)
{
    <tr>
    <td>
    <label class="FixedOPpointer ChangePID" data-id="@(item.PurchaseOrderID)" id="lblChangePID@(item.PurchaseOrderID)">@item.Ref @item.CallCentre</label>
    <input type="text" id="ChangePID@(item.PurchaseOrderID)" class="hiddennewvals" value="@(item.Ref)" data-id="@(item.PurchaseOrderID)" data-mode="P" style="font-family: 'Lucida Console'; font-size: 15px; font-weight: bold; color: #FF0000; text-transform: uppercase;"/>
    </td>
    <td>
    <label class="FixedOP ChangeQuantity" data-id="@(item.PurchaseOrderID)" id="lblChangeQTY@(item.PurchaseOrderID)">@item.VolumeOrdered</label>
    <input type="text" id="ChangeQuantity@(item.PurchaseOrderID)" class="hiddennewvals" value="@(item.VolumeOrdered)" data-id="@(item.PurchaseOrderID)" data-mode="Q" style="font-family: 'Lucida Console'; font-size: 15px; font-weight: bold; color: #FF0000; width:60px; text-transform: uppercase;"/>
    </td>
    <td>
    <label class="FixedOP ChangeNewLeadsSupplied" data-id="@(item.PurchaseOrderID)" id="lblChangeNewLeadsSupplied@(item.PurchaseOrderID)">@item.NewLeadsSupplied</label>
    <input type="text" id="ChangeNewLeadsSupplied@(item.PurchaseOrderID)" class="hiddennewvals" value="@(item.NewLeadsSupplied)" data-id="@(item.PurchaseOrderID)" data-mode="N" style="font-family: 'Lucida Console'; font-size: 15px; font-weight: bold; color: #FF0000; width:60px; text-transform: uppercase;"/>
    </td>
    <td>
    <label class="FixedOP">@item.NewLeadDeficit</label>
    </td>
    <td>
    <label class="FixedOP ChangeReturns" data-id="@(item.PurchaseOrderID)" id="lblChangeReturns@(item.PurchaseOrderID)">@item.Returns</label>
    <input type="text" id="ChangeReturns@(item.PurchaseOrderID)" class="hiddennewvals" value="@(item.Returns)" data-id="@(item.PurchaseOrderID)" data-mode="R" style="font-family: 'Lucida Console'; font-size: 15px; font-weight: bold; color: #FF0000; width:60px; text-transform: uppercase;"/>
    </td>
    <td>
    <label class="FixedOP ChangeInvalidReturns" data-id="@(item.PurchaseOrderID)" id="lblChangeInvalidReturns@(item.PurchaseOrderID)">@item.InvalidReturns</label>
    <input type="text" id="ChangeInvalidReturns@(item.PurchaseOrderID)" class="hiddennewvals" value="@(item.InvalidReturns)" data-id="@(item.PurchaseOrderID)" data-mode="I" style="font-family: 'Lucida Console'; font-size: 15px; font-weight: bold; color: #FF0000; width:60px; text-transform: uppercase;"/>
    </td>
	<td>
    <label class="FixedOP ChangeReplacementsSupplied" data-id="@(item.PurchaseOrderID)" id="lblChangeReplacementsSupplied@(item.PurchaseOrderID)">@item.ReplacementsSupplied</label>
    <input type="text" id="ChangeReplacementsSupplied@(item.PurchaseOrderID)" class="hiddennewvals" value="@(item.ReplacementsSupplied)" data-id="@(item.PurchaseOrderID)" data-mode="S" style="font-family: 'Lucida Console'; font-size: 15px; font-weight: bold; color: #FF0000; width:60px; text-transform: uppercase;"/>
    </td>
	<td><label class="FixedOP">@item.ReplacementsDeficit</label></td>
	<td><label class="FixedOP">@item.TotalDeficit</label></td>
    <td>
    <input type="checkbox" id="ClientPaid@(item.PurchaseOrderID)" class="ClientPaid" 
    @if(item.ClientPaid==true)
    {
    @:checked
    }
     data-id="@(item.PurchaseOrderID)" data-mode="A" />
    </td>
    <td>
    <input type="checkbox" name="Visible" data-id="@(item.PurchaseOrderID)" class="poVisible" 
    @if(item.Visible==true) 
    {
     @:checked   
    }
    />
    </td>
    <td><img src="~/Images/Edit.png" id="Edit@(item.PurchaseOrderID)" class="editpo" data-purchaseorderid="@(item.PurchaseOrderID)" style="width: 15px; height: 15px; cursor: pointer;" /></td>
    </tr>
}
<tr>
<td><label>&nbsp;</label></td>
<td><label class="RunningTotal">@Model.rT.VolumeOrdered</label></td>
<td><label class="RunningTotal">@Model.rT.NewLeadsSupplied</label></td>
<td><label class="RunningTotal">@Model.rT.NewLeadDeficit</label></td>
<td><label class="RunningTotal">@Model.rT.Returns</label></td>
<td><label class="RunningTotal">@Model.rT.InvalidReturns</label></td>
<td><label class="RunningTotal">@Model.rT.ReplacementsSupplied</label></td>
<td><label class="RunningTotal">@Model.rT.ReplacementsDeficit</label></td>
<td><label class="RunningTotal">@Model.rT.TotalDeficit</label></td> 
<td><label>&nbsp;</label></td>
<td><label>&nbsp;</label></td>
<td></td>
</tr>

</table>
</div>

<div class="spacer"></div>
<input type="button" id="btnAddPO" value="Create Purchase Order" style="font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 12px" />

</div>

<script type="text/javascript">
    $(document).ready(function () {

        $("#createpurchaseorder").hide();
        $(".hiddennewvals").hide();

        $('.editpo').click(function () {
            $("#formCreatePO").dialog("close");
            var contactURL = '@(Url.Action("Edit", "PurchaseOrder"))?id=' + $(this).data("purchaseorderid");
            window.location.href = contactURL;
        });

        $('#btnAddPO').click(function () {
            $('#purchaseorders').load('@(Url.Action("CreatePurchaseOrder", "Leads"))?leadID=@(Model.Lead.LeadID)');
        });

        $('.ChangePIDx').click(function () {
            var curElement = $(this).attr("id");
            var curPID = $(this).data("id");
            var curVal = $(this).val();
            $(this).hide();
            $("#ChangePID" + curPID).show();
        });

        $('.ChangeQuantityx').click(function () {
            var curElement = $(this).attr("id");
            var curPID = $(this).data("id");
            var curVal = $(this).val();
            $(this).hide();
            $("#ChangeQuantity" + curPID).show();
        });

        $('.ChangeNewLeadsSuppliedx').click(function () {
            var curElement = $(this).attr("id");
            var curPID = $(this).data("id");
            var curVal = $(this).val();
            $(this).hide();
            $("#ChangeNewLeadsSupplied" + curPID).show();
        });

        $('.ChangeReturnsx').click(function () {
            var curElement = $(this).attr("id");
            var curPID = $(this).data("id");
            var curVal = $(this).val();
            $(this).hide();
            $("#ChangeReturns" + curPID).show();
        });

        $('.ChangeInvalidReturnsx').click(function () {
            var curElement = $(this).attr("id");
            var curPID = $(this).data("id");
            var curVal = $(this).val();
            $(this).hide();
            $("#ChangeInvalidReturns" + curPID).show();
        });

        $('.ChangeReplacementsSuppliedx').click(function () {
            var curElement = $(this).attr("id");
            var curPID = $(this).data("id");
            var curVal = $(this).val();
            $(this).hide();
            $("#ChangeReplacementsSupplied" + curPID).show();
        });

        $('.ClientPaid').change(function () {
            var ischecked = $(this).is(':checked') ? 1 : 0;
            var theUrl = '@Url.Action("ChangeClientPaid", "Leads")?id=' + $(this).data("id") + '&qty=' + ischecked;
            $.ajax({
                url: theUrl,
                type: "POST",
                success: function (data) {
                    alert("Modified");
                }
            });
        });

        $('.poVisible').change(function () {
            var ischecked = $(this).is(':checked') ? 1 : 0;
            var theUrl = '@Url.Action("ChangePOVisibility", "Leads")?id=' + $(this).data("id") + '&qty=' + ischecked;
            $.ajax({
                url: theUrl,
                type: "POST",
                success: function (data) {
                    alert("Modified");
                }
            });
        });

        $('.hiddennewvals').blur(function () {
            switch ($(this).data("mode")) {
                case "P":
                    var theUrl = '@Url.Action("ChangePID", "Leads")?id=' + $(this).data("id") + '&pid=' + $(this).val();
                    $.ajax({
                        url: theUrl,
                        type: "POST",
                        success: function (data) {
                            alert("Modified");
                        }
                    });
                    $('#lblChangePID' + $(this).data("id")).text($(this).val());
                    $('#lblChangePID' + $(this).data("id")).show();
                    $(this).hide();
                    break;
                case "Q":
                    var theUrl = '@Url.Action("ChangeQty", "Leads")?id=' + $(this).data("id") + '&qty=' + $(this).val();
                    $.ajax({
                        url: theUrl,
                        type: "POST",
                        success: function (data) {
                            alert("Modified");
                        }
                    });
                    $('#lblChangeQTY' + $(this).data("id")).text($(this).val());
                    $('#lblChangeQTY' + $(this).data("id")).show();
                    $(this).hide();
                    break;
                case "N":
                    var theUrl = '@Url.Action("ChangeNewLeadsSupplied", "Leads")?id=' + $(this).data("id") + '&qty=' + $(this).val();
                    $.ajax({
                        url: theUrl,
                        type: "POST",
                        success: function (data) {
                            alert("Modified");
                        }
                    });
                    $('#lblChangeNewLeadsSupplied' + $(this).data("id")).text($(this).val());
                    $('#lblChangeNewLeadsSupplied' + $(this).data("id")).show();
                    $(this).hide();
                    break;
                case "R":
                    var theUrl = '@Url.Action("ChangeReturns", "Leads")?id=' + $(this).data("id") + '&qty=' + $(this).val();
                    $.ajax({
                        url: theUrl,
                        type: "POST",
                        success: function (data) {
                            alert("Modified");
                        }
                    });
                    $('#lblChangeReturns' + $(this).data("id")).text($(this).val());
                    $('#lblChangeReturns' + $(this).data("id")).show();
                    $(this).hide();
                    break;
                case "I":
                    var theUrl = '@Url.Action("ChangeInvalidReturns", "Leads")?id=' + $(this).data("id") + '&qty=' + $(this).val();
                    $.ajax({
                        url: theUrl,
                        type: "POST",
                        success: function (data) {
                            alert("Modified");
                        }
                    });
                    $('#lblChangeInvalidReturns' + $(this).data("id")).text($(this).val());
                    $('#lblChangeInvalidReturns' + $(this).data("id")).show();
                    $(this).hide();
                    break;
                case "S":
                    var theUrl = '@Url.Action("ChangeReplacementsSupplied", "Leads")?id=' + $(this).data("id") + '&qty=' + $(this).val();
                    $.ajax({
                        url: theUrl,
                        type: "POST",
                        success: function (data) {
                            alert("Modified");
                        }
                    });
                    $('#lblChangeReplacementsSupplied' + $(this).data("id")).text($(this).val());
                    $('#lblChangeReplacementsSupplied' + $(this).data("id")).show();
                    $(this).hide();
                    break;
                default:

            }
            $('#purchaseorders').load('@(Url.Action("PurchaseOrder", "Leads"))?id=@(Model.Lead.LeadID)');

        });
    });
</script>